/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.prolificinteractive.materialcalendarview.sample;

import ohos.aafwk.content.Intent;
import ohos.agp.utils.Color;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.prolificinteractive.materialcalendarview.sample.decorators.EventDecorator;
import com.prolificinteractive.materialcalendarview.sample.decorators.HighlightWeekendsDecorator;
import com.prolificinteractive.materialcalendarview.sample.decorators.MySelectorDecorator;

import org.threeten.bp.LocalDate;
import org.threeten.bp.Month;

import java.util.ArrayList;

/**
 * @since 2021-07-12
 */
public class BasicAbilityDecorated extends BaseAbility implements OnDateSelectedListener {

    private MaterialCalendarView widget;
    private MySelectorDecorator mySelectorDecorator;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        toolbarTitle.setText("Calendar with Decorators");
        widget = (MaterialCalendarView) findComponentById(ResourceTable.Id_calendarView);
        widget.setOnDateChangedListener(this);
        widget.setShowOtherDates(7);
        final LocalDate instance = LocalDate.now();
        final LocalDate min = LocalDate.of(instance.getYear(), Month.JANUARY, 1);
        final LocalDate max = LocalDate.of(instance.getYear(), Month.DECEMBER, 31);
        widget.state().edit().setMinimumDate(min).setMaximumDate(max).commit();
        mySelectorDecorator = new MySelectorDecorator(this);
        widget.isBigSize(true);
        widget.addDecorators(mySelectorDecorator, new HighlightWeekendsDecorator());
        widget.setSelectedDate(instance);


        getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LocalDate temp = LocalDate.now().minusMonths(2);
                final ArrayList<CalendarDay> dates = new ArrayList<>();
                for (int i = 0; i < 30; i++) {
                    final CalendarDay day = CalendarDay.from(temp);
                    dates.add(day);
                    temp = temp.plusDays(5);
                }
                new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                    @Override
                    public void run() {
                        widget.addDecorator(new EventDecorator(Color.RED.getValue(), dates));
                    }
                });
            }
        });
    }

    @Override
    int contentLayoutRes() {
        return ResourceTable.Layout_ability_basic;
    }

    @Override
    public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {
    }
}


